$(document).ready(function() {
	//ajaxform
	var options = { 
		beforeSubmit:showRequest,//表单验证方式
        success:showResponse  // post-submit callback 
    }; 

    $('#ajaxForm').ajaxForm(options); 

    function showResponse(responseText, statusText)  {
		if (statusText == 'success') {
			var responseText=eval('('+responseText+')');
			if(responseText.state == 'SUCCESS'){
				window.location.href = responseText.url;
			}else{
				$("#form-btn").removeAttr('disabled');
		 		$("#msg").html(responseText.state);	
			}			
		}else{
			$("#form-btn").removeAttr('disabled');
		 	$("#msg").html('Lost Service');			
		}
	} 

	function showRequest(){
		 validata = $("form.form-validata").valid();
		 if (validata){
		 	$("#form-btn").attr('disabled','disabled');
		 	$("#msg").html('<img src="/tpl/Home/common/image/loadding.gif">');
		 }
		 return validata;
	}

	//validation验证
	$("form.form-validata").validate({
		errorElement: "p",
        errorClass: "error",
	})

	$.validator.addMethod("pwd",function(value,element){
        return this.optional(element) || testPass(value);
    },"Password must be at least 8 characters and include at least 3 of the following requirement<br/>Contain at least 1 English uppercase chatacters (A-Z)<br/>Contain at least 1 English lowercase characters (a-z)<br/>Contain at least 1 numeric base 10 digits (0-9)<br/>Contain at least 1 non-alphanumeric characters (for example: ?,!,&,%)");

    $.validator.addMethod("phone",function(value,element){
        return this.optional(element) || testPhone(value);
    },"Phone format error");


    //resend
    $("#a_resend").click(function(){
        $(this).attr('disabled','disabled');
        $("#sendEmail").html('<img src="/tpl/Home/common/image/loadding.gif">');
        $.ajax({
            url : 'index.php?c=Register&a=resend',
            type : 'post',
            data : {email:$("#email").html()},
            dataType : 'text',
            success : function(response){
                $(this).removeAttr('disabled');
                $("#sendEmail").html(response);
            }
        })
    })

});


//密码
function testPass(str){
    var rC = {
        lW:'[a-z]',
        uW:'[A-Z]',
        nW:'[0-9]',
        sW:'[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E]'
    };
    function Reg(str, rStr){
        var reg = new RegExp(rStr);
        if(reg.test(str)) return true;
        else return false;
    }
    if(str.length < 8){
        //document.title = '您的密码长度太短';
        return false;
    }else{
        var tR = {
            l:Reg(str, rC.lW),
            u:Reg(str, rC.uW),
            n:Reg(str, rC.nW),
            s:Reg(str, rC.sW)
        };
        if((tR.l && tR.u && tR.n) || (tR.l && tR.u && tR.s) || (tR.s && tR.u && tR.n) || (tR.s && tR.l && tR.n)){
            //document.title = '密码符合要求';
            return true;
        }else{
            //document.title = '您的密码必须含有“小写字母”、“大写字母”、“数字”、“特殊符号”中的任意三种';
            return false;
        }
    }
}

function testPhone(v){
    if(v==""){
        return false;
    }else{
        var pattern = /^((?:\d{9})|(?:\d{3}-\d{4})|(?:\d{3}-\d{3}-\d{4})|[0-9]{10})$/;
        flag = pattern.test(v);
        if(!flag){
            return false;
        }else{
            return true;
        }
    }
}